<template>
    <el-container>
        <el-header>
        </el-header>
        <el-main v-loading="loading">
            <el-row>
                <main-content></main-content>
            </el-row>
        </el-main>
    </el-container>
</template>

<script>
    import {mapState} from 'vuex';
    import MainContent from './components/MainContent';
    import AgoraRTC from 'agora-rtc-sdk'
    import { Message } from 'element-ui';

    export default {
        name: 'app',
        components: {
            MainContent,
        },
        computed: mapState({

        }),
        data() {
            return {
                loading: false,
            }
        },
        async created() {
            try {
                let live_access_token = '006a8a39c86e90847779e6e82873e17b2fcIADSVogT49RhAW7XACe+Fmct939xmIbY5uY9YSMDoM7ad2vs3lkAAAAAEACxGAAA24kxXQEAAQAAAAAA';
                let agora_channel_id = '121';
                let uid = 121;

                var client = AgoraRTC.createClient({mode: 'live', codec: 'h264'});

                client.on('peer-online',function (msg) {
                    console.log('peer-online',msg);
                });
                client.on('peer-leave',function (msg) {
                    console.log('peer-leave',msg);
                });

                client.init("43f7e5ee5018497e9a0918acb897c23b", function () {
                    console.log('AgoraRTC client initialized');
                }, function (err) {
                    console.log('AgoraRTC client init failed', err);
                });

                let localStream = null;

                await new Promise(((resolve,reject) => {
                    console.log('token :::', live_access_token, agora_channel_id, uid);
                    client.join(null, agora_channel_id, null, function (uid) {
                        console.log('User ' + uid + ' join channel successfully');
                        resolve();


                    }, function (err) {
                        console.log('Join channel failed', err);
                        reject(err);
                    });


                }));

                localStream = AgoraRTC.createStream({
                    streamID: uid,
                    audio: true,
                    video: false,
                    screen: false,
                });
                localStream.setAudioProfile('speech_standard');
                localStream.init(function () {
                    console.log('getUserMedia successfully');
                    localStream.play('agora_local');
                    console.log("publish after iniit");
                    client.publish(localStream, function (err) {
                        console.log('Publish local stream error: ' + err);
                    });

                }, function (err) {
                    console.log('getUserMedia failed', err);
                    Message.error({message:'Failed to obtain microphone authorization, please refresh and retry.',duration : 360000})
                });
                console.log('startRecord');

            } catch (e) {
                this.$message.error(e.message);
            } finally {
                this.loading = false;
            }
        },
        methods: {

        }
    }
</script>

<style lang="scss">
    .live-room,.live-main{
        height: 100%;
    }

    .el-collapse-item{
        margin-bottom: 15px;
    }

    .left-content .el-collapse-item__header{
        border-radius: 5px;
    }

    .left-content .el-collapse-item__header.is-active{
        border-bottom-left-radius: 0px;
        border-bottom-right-radius: 0px;
    }

</style>
